package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Search_Exact_Entity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 */

public class Search_Exact_Adapter extends BaseAdapter {
    private Context context;
    private List<Search_Exact_Entity.DataBean> dataBeanList;

    public Search_Exact_Adapter(Context context, List<Search_Exact_Entity.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    public void freshenAdapter(List<Search_Exact_Entity.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataBeanList != null ? dataBeanList.size() : 0;
    }

    @Override
    public Search_Exact_Entity.DataBean getItem(int position) {
        return dataBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        exectSearchViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_fragment_item, parent, false);
            holder = new Search_Exact_Adapter.exectSearchViewHolder();
            holder.commodity_details = (TextView) convertView.findViewById(R.id.commodity_details);
            holder.commodity_mbn_details = (TextView) convertView.findViewById(R.id.commodity_mbn_details);
            holder.commodity_name = (TextView) convertView.findViewById(R.id.commodity_name);
            holder.commodity_money = (TextView) convertView.findViewById(R.id.commodity_money);
            holder.commodity_shop_money = (TextView) convertView.findViewById(R.id.commodity_shop_money);
            holder.commodity_picture = (ImageView) convertView.findViewById(R.id.commodity_picture);
            holder.commodity_gouwuche = (ImageView) convertView.findViewById(R.id.commodity_gouwuche);
            holder.commodity_level = (TextView) convertView.findViewById(R.id.commodity_level);
            holder.commodity_saled = (TextView) convertView.findViewById(R.id.commodity_saled);
            convertView.setTag(holder);
        } else {
            holder = (Search_Exact_Adapter.exectSearchViewHolder) convertView.getTag();
        }
//        SpannableString spanMoney = new SpannableString();
//        AbsoluteSizeSpan span = new AbsoluteSizeSpan(50);
//        spanMoney.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        if (dataBeanList.get(position).getMbn_detailsType() == 1) {
            holder.commodity_money.setText("¥" + dataBeanList.get(position).getMoney() + "/斤");
        } else if (dataBeanList.get(position).getMbn_detailsType() == 2) {
            holder.commodity_money.setText("¥" + dataBeanList.get(position).getMoney() + "/个");
        }
        holder.commodity_saled.setText("已售:" + dataBeanList.get(position).getBuyNum());
        holder.commodity_gouwuche.setVisibility(View.GONE);
        SpannableString add_center_line = new SpannableString("商超价/¥" + dataBeanList.get(position).getShop_money());
        add_center_line.setSpan(new StrikethroughSpan(), 0, add_center_line.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.commodity_shop_money.setText(add_center_line);
        holder.commodity_name.setText(dataBeanList.get(position).getName());
        holder.commodity_mbn_details.setText(dataBeanList.get(position).getMbn_details());
        holder.commodity_details.setText(dataBeanList.get(position).getDetails());
        holder.commodity_level.setText(dataBeanList.get(position).getLevel());
        if (dataBeanList.get(position).getImg() != null) {
            Picasso.with(context).load(dataBeanList.get(position).getImg()).placeholder(R.mipmap.zhanzhanzhan).into(holder.commodity_picture);
        }
        return convertView;
    }

    class exectSearchViewHolder {
        TextView commodity_name;
        TextView commodity_mbn_details;
        TextView commodity_details;
        TextView commodity_money;
        TextView commodity_shop_money;
        ImageView commodity_picture;
        ImageView commodity_gouwuche;
        TextView commodity_level;
        TextView commodity_saled;
    }
}
